package com.jhgsys.internal.accident.service.impl;

import com.jhgsys.internal.accident.entity.AccidentSurveyTeamUser;
import com.jhgsys.internal.accident.mapper.AccidentSurveyTeamUserMapper;
import com.jhgsys.internal.accident.service.IAccidentSurveyTeamUserService;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.utils.FebsUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.toolkit.StringPool;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 *  Service实现
 *
 * @author zhangwei
 * @date 2024-09-11 15:21:37
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class AccidentSurveyTeamUserServiceImpl extends ServiceImpl<AccidentSurveyTeamUserMapper, AccidentSurveyTeamUser> implements IAccidentSurveyTeamUserService {

    /**
     * 查询（分页）
     * @author zhangwei
     * @date 2024-09-11 15:21:37
     * @param request QueryRequest
     * @param accidentSurveyTeamUser accidentSurveyTeamUser
     * @return IPage<AccidentSurveyTeamUser>
    */
    @Override
    public IPage<AccidentSurveyTeamUser> findAccidentSurveyTeamUserPage(QueryRequest request, AccidentSurveyTeamUser accidentSurveyTeamUser) {
        LambdaQueryWrapper<AccidentSurveyTeamUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AccidentSurveyTeamUser::getDeletedFlg, 0);
        // TODO 设置查询条件

        Page<AccidentSurveyTeamUser> page = new Page<>(request.getPageNum(), request.getPageSize());
        FebsUtil.pageWrapper(page, request);
        return this.page(page, queryWrapper);
    }

    /**
     * 查询（所有）
     * @author zhangwei
     * @date 2024-09-11 15:21:37
     * @param accidentSurveyTeamUser accidentSurveyTeamUser
     * @return List<AccidentSurveyTeamUser>
     */
    @Override
    public List<AccidentSurveyTeamUser> findAccidentSurveyTeamUserList(AccidentSurveyTeamUser accidentSurveyTeamUser) {
	    LambdaQueryWrapper<AccidentSurveyTeamUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AccidentSurveyTeamUser::getDeletedFlg, 0);
		// TODO 设置查询条件
        if(StringUtils.isNotBlank(accidentSurveyTeamUser.getSurveyTeamId())){
            queryWrapper.eq(AccidentSurveyTeamUser::getSurveyTeamId,accidentSurveyTeamUser.getSurveyTeamId());
        }
		return this.baseMapper.selectList(queryWrapper);
    }

    /**
    * 根据ID查询
    * @author zhangwei
    * @date 2024-09-11 15:21:37
    * @param id
    * @return AccidentSurveyTeamUser
    */
    @Override
    public AccidentSurveyTeamUser findAccidentSurveyTeamUserById(String id){
        return this.baseMapper.selectById(id);
    }

    /**
     * 新增
     * @author zhangwei
     * @date 2024-09-11 15:21:37
     * @param accidentSurveyTeamUser accidentSurveyTeamUser
     */
    @Override
    @Transactional
    public void createAccidentSurveyTeamUser(AccidentSurveyTeamUser accidentSurveyTeamUser) {
        accidentSurveyTeamUser.setCreatedTime(new Date());
        this.save(accidentSurveyTeamUser);
    }

    /**
     * 修改
     * @author zhangwei
     * @date 2024-09-11 15:21:37
     * @param accidentSurveyTeamUser accidentSurveyTeamUser
     */
    @Override
    @Transactional
    public void updateAccidentSurveyTeamUser(AccidentSurveyTeamUser accidentSurveyTeamUser) {
        accidentSurveyTeamUser.setUpdatedTime(new Date());
        this.saveOrUpdate(accidentSurveyTeamUser);
    }

    /**
     * 逻辑删除
     * @author zhangwei
     * @date 2024-09-11 15:21:37
     * @param accidentSurveyTeamUser accidentSurveyTeamUser
     * @param accidentSurveyTeamUserIds accidentSurveyTeamUserIds
     */
    @Override
    @Transactional
    public void deleteAccidentSurveyTeamUser(AccidentSurveyTeamUser accidentSurveyTeamUser,String accidentSurveyTeamUserIds) {
        List<String> list = Arrays.asList(accidentSurveyTeamUserIds.split(StringPool.COMMA));
        this.baseMapper.update(accidentSurveyTeamUser,new LambdaQueryWrapper<AccidentSurveyTeamUser>().in(AccidentSurveyTeamUser::getSurveyTeamUserId,list));
	}

    /**
    * 物理删除
    * @author zhangwei
    * @date 2024-09-11 15:21:37
    * @param accidentSurveyTeamUserIds accidentSurveyTeamUserIds
    */
    @Override
    @Transactional
    public void physicallyDeleteAccidentSurveyTeamUser(String accidentSurveyTeamUserIds) {
        List<String> list = Arrays.asList(accidentSurveyTeamUserIds.split(StringPool.COMMA));
        LambdaQueryWrapper<AccidentSurveyTeamUser> wapper = new LambdaQueryWrapper<>();
        wapper.in(AccidentSurveyTeamUser::getSurveyTeamUserId,list);
        this.remove(wapper);
    }

    /**
     * 根据检查小组id删除
     *
     * @param teamIds
     * @author zhangwei
     * @date 2024-09-11 15:21:37
     */
    @Override
    public void deleteByTeamIds(String teamIds) {
        List<String> list = Arrays.asList(teamIds.split(StringPool.COMMA));
        LambdaQueryWrapper<AccidentSurveyTeamUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(AccidentSurveyTeamUser::getSurveyTeamId,list);
        this.remove(wrapper);
    }
}
